﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CreatePGSQLDataScript
{
    public partial class ReadApfield : Form
    {
        public ReadApfield()
        {
            InitializeComponent();
        }
        DataAccess.SqlProvider db;
        private void ReadApfield_Load(object sender, EventArgs e)
        {
           
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);

        }
        private void btnRead_Click(object sender, EventArgs e)
        {
            //先删除
            string sqlDeleteText = "delete apfield;";
            db.ExecuteSQL(sqlDeleteText);


            int iCount = 0;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(txtFile.Text.Trim());
            StringBuilder sqlTextColl = new StringBuilder();
            string existFields = "msgval,msgtext,DDLConditionsg,definedf,JoinConditions,msgvalhead";
            progressBar1.Maximum = xmlDoc.ChildNodes[1].ChildNodes.Count -1;
            foreach(XmlNode  row in xmlDoc.ChildNodes[1].ChildNodes)
            {
                string fields="";
                string values="";
                string sqlTemp = "insert into apfield({0}) values({1});";

                if (row.ChildNodes.Count == 1)
                {
                    continue;
                }

                for (int col =0; col < row.ChildNodes.Count; col++)
                {
                    string fieldname = row.ChildNodes[col].Name;
                    string fieldvalue = row.ChildNodes[col].InnerText;

                    if (fieldvalue.Length > 200 && !(existFields.Contains(fieldname)))
                    {
                        int i = 10;
                        i++;
                    }

                    fieldvalue = fieldvalue.Replace("'", "''");

                    fields += "," + fieldname;
                    values += ",'" + fieldvalue + "'";

                }

                fields = fields.Substring(1);
                values = values.Substring(1);

                string sqlText=string.Format(sqlTemp,fields,values);
                sqlTextColl.Append(sqlText);

                if (iCount % 100 == 0)
                {
                    db.ExecuteSQL(sqlTextColl.ToString());
                    sqlTextColl = new StringBuilder();
                }


                iCount++;
                textBox1.Text =progressBar1.Value.ToString()+"/"+ progressBar1.Maximum.ToString();
                progressBar1.Value = iCount;
                Application.DoEvents();
            }
            if (sqlTextColl.Length > 0)
            {
                db.ExecuteSQL(sqlTextColl.ToString());
                textBox1.Text = progressBar1.Value.ToString() + "/" + progressBar1.Maximum.ToString();
                sqlTextColl = new StringBuilder();
            }
        }
    }
}
